package com.yunfeng.supercall.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.ly.quickdev.library.adapter.ListBaseAdapter.ViewHolder;
import com.ly.quickdev.library.utils.JsonUtils;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX.Req;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.AdsBean;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.bean.PhoneContactsBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.bean.SystemContactsBean;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;
import com.yunfeng.supercall.manager.AdsManager;
import com.yunfeng.supercall.manager.ManagerFactory;
import com.yunfeng.supercall.manager.ShareManager;

/**
 * 添加超友
 * 
 * @author 洋
 * 
 */
public class AddSuperFriendsActivity extends
		BaseStickyHeaderListActivity<ContactsBaseBean> {
	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		if (position == 0) {
			return new View(getActivity());
		} else {
			return super.getHeaderView(position, convertView, parent);
		}
	}

	/** 微博微博分享接口实例 */
	// private IWeiboShareAPI mWeiboShareAPI = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 创建微博分享接口实例
		setTitleBar(R.string.title_add_super_friends);
	}

	@Override
	public void onSupportContentChanged() {
		super.onSupportContentChanged();
		AdsManager mAdsManager = (AdsManager) ManagerFactory
				.createManager(AdsBean.class);
		mAdsManager.addAdsHeader(mListView.getWrappedList(), 0);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		position = position - mListView.getWrappedList().getHeaderViewsCount();
		if (position == 0) {
			startActivity(new Intent(this, NearBySuperFriendsListActivity.class));
		} else if (position == 1) {
			startActivity(new Intent(this, NearBySuperFriendsListActivity.class)
					.putExtra(KEY_DATA, 1));
		} else if (position == 2) {
			startActivity(new Intent(this, NearByCircleListActivity.class));
		}
		ContactsBaseBean base = mList.get(position);
		if (base instanceof PhoneContactsBean) {
			searchFriends(base.getPhone(), base);
		}
		if (base instanceof SystemContactsBean) {
			int img = ((SystemContactsBean) base).getImgId();
			// if (img == R.drawable.ic_super_friend_group) {
			// startActivity(new Intent(getActivity(),
			// SuperFriendsGroupActivity.class));
			// }
			if (img == R.drawable.ic_my_circle) {
				startActivity(new Intent(getActivity(),
						MyCircleListActivity.class));
			} else if (img == R.drawable.ic_super_friend_group) {
				startActivity(new Intent(getActivity(),
						FriendsGroupListActivity.class));
			} else if (img == R.drawable.ic_new_super_friend) {
				startActivity(new Intent(getActivity(),
						NewSuperFriendsListActivity.class));
			} else if (img == R.drawable.ic_weibo_friends) {
				ShareManager.shareToWeiBo(getActivity());
			} else if (img == R.drawable.ic_sms) {
				ShareManager.shareToSms(getActivity());
			} else if (img == R.drawable.ic_wechat_circle) {
				BaseActivity.shareByWeiXin(getActivity(), Req.WXSceneTimeline);
			} else if (img == R.drawable.ic_wechat_friends) {
				BaseActivity.shareByWeiXin(getActivity(), Req.WXSceneSession);
			} else if (img == R.drawable.ic_qq_friends) {
				ShareManager.shareToQQ(getActivity());

			}
		}
	}

	private void searchFriends(String phone, final ContactsBaseBean base) {
		JSONObject json = BaseActivity.getBaseParams(getActivity());
		try {
			json.put("i_key", phone);
			json.put("i_type", 0);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mHttpClient.post(FUNCTION_SEARCH_USER, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveData(String data, String msg) {
				try {
					SuperFriendsBean mSuperFriends = JsonUtils.parse(
							data.substring(1, data.length() - 1),
							SuperFriendsBean.class);
					if (mSuperFriends != null
							&& mSuperFriends.getVo_id() != null) {
						startActivity(new Intent(getActivity(),
								FriendsDetailActivity.class).putExtra(KEY_DATA,
								mSuperFriends));
					} else {
						startActivity(new Intent(getActivity(),
								NotAFriendsActivity.class).putExtra(KEY_DATA,
								base));
					}
				} catch (Exception e) {
					showToast(R.string.super_number_not_found);
				}
			}

			@Override
			public void onReceiveError(String msg, String errorCode) {
				showToast(R.string.super_number_not_found);
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public void loadData() {
		mList.addAll(SystemContactsBean.getAddSystemContactsList(this));
		mList.addAll(SystemContactsBean.getSheJiaoPingtai(getActivity()));
		mList.addAll(ManagerFactory.createManager(PhoneContactsBean.class)
				.getList());
		setListAdapter();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ContactsBaseBean base = mAdapter.getItem(position);
		int type = base.getContactsType();
		if (convertView == null) {
			switch (type) {
			case ContactsBaseBean.TYPE_NORMAL_CONTACTS:
				convertView = getLayoutInflater().inflate(
						R.layout.list_item_phone_contacts, null);
				break;
			case ContactsBaseBean.TYPE_SUPER_CONTACTS:
				convertView = getLayoutInflater().inflate(
						R.layout.list_item_phone_contacts, null);
				break;
			case ContactsBaseBean.TYPE_SYSTEM_CONTACTS:
				convertView = getLayoutInflater().inflate(
						R.layout.list_item_system_friends, null);
				break;
			}
		}
		ContactsBaseBean.setupSingleLineContactsView(convertView, base);
		if (base instanceof PhoneContactsBean) {
			final ImageView iv = ViewHolder.get(convertView, R.id.iv);
			iv.setImageBitmap(AppContext.BITMAP);
			final ImageView iv1 = ViewHolder.get(convertView, R.id.is_super);
			if (iv1 != null) {
				iv1.setVisibility(((PhoneContactsBean) base).isSuperNum() ? View.VISIBLE
						: View.INVISIBLE);
			}
		}
		return convertView;
	}

}
